package com.more.offer.designpatterns.decorator;

import java.io.*;

/**
 * @author pinoc
 * @date 2021/10/29
 * @description  没有使用装饰器模式的简单读写
 */
public class SimpleDataReadWrite implements DataSource{
	private String filePath;

	public SimpleDataReadWrite(String filePath) {
		this.filePath = filePath;
	}

	@Override
	public void writeData(String data) {
		System.out.println("读取----start----");
		File file = new File(filePath);
		try {
			OutputStream outputStream = new FileOutputStream(file);
			outputStream.write(data.getBytes(),0 , data.length());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public String readData() {
		char[] buffer = null;
		File file = new File(filePath);
		try (FileReader reader = new FileReader(file)) {
			buffer = new char[(int) file.length()];
			reader.read(buffer);
		} catch (IOException ex) {
			System.out.println(ex.getMessage());
		}
		return new String(buffer);
	}
}
